package net.shopxx.excel.conf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.shopxx.excel.excel.Column;
import net.shopxx.excel.excel.Excel;
import net.shopxx.excel.util.XmlUtils;

import org.dom4j.Document;
import org.dom4j.Element;

public class ExcelFileXmlConfigure {

	Map<String, Excel> map;

	public ExcelFileXmlConfigure() {
		map = new HashMap<String, Excel>();
	}

	@SuppressWarnings("unchecked")
	public Map<String, Excel> configure(String path) {
		Document document = XmlUtils.getDocument(path);
		Element root = document.getRootElement();
		List<Element> files = root.elements();
		Excel excel;
		for (Iterator<Element> iterator = files.iterator(); iterator.hasNext(); map.put(excel.getName(), excel)) {
			Element file = iterator.next();
			excel = new Excel();
			List<Element> columns = file.elements();
			excel.setName(file.attributeValue("name"));
			excel.setClassName(file.attributeValue("class"));
			Map<String, Column> mapcol = new HashMap<String, Column>();
			List<String> columnsName = new ArrayList<String>();
			String column;
			for (Iterator<Element> iterator1 = columns.iterator(); iterator1.hasNext(); columnsName.add(column)) {
				Element colum = iterator1.next();
				column = colum.attributeValue("column").trim();
				String field = colum.attributeValue("field").trim();
				String type = colum.attributeValue("type").trim();
				Column c = new Column(column, field, type);
				mapcol.put(column, c);
			}

			excel.setColumns(mapcol);
			excel.setColumnsName(columnsName);
		}

		return map;
	}
}